//
//  YHMineViewController.swift
//  SortProject
//
//  Created by ALH000355 on 2022/7/29.
//

import UIKit

class YHMineViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        uiConfig()
    }
    
    func uiConfig() {
        view.backgroundColor = colorGray244
        
        createHeader()
        createTableView()
    }
    func createTableView() {
        tableView = UITableView.init(frame: CGRect(x: 0, y: 280, width: screenWidth, height: screenHeight - 300), style: .grouped)
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "YHMineViewController")
    }
    func createHeader() {
        let header = UIView.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 274))
        header.backgroundColor = colorTheme
        view.addSubview(header)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "YHMineViewController")! as UITableViewCell
        cell.textLabel?.text = "11111"
        cell.selectionStyle = .none
        cell.backgroundColor = colorGray244
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath){
            // 圆角弧度半径
            let cornerRadius: CGFloat = 2.0
            // 设置cell的背景色为透明，如果不设置这个的话，则原来的背景色不会被覆盖
            cell.backgroundColor = UIColor.clear
            // 创建一个shapeLayer
            let layer = CAShapeLayer()
            // 创建一个可变的图像Path句柄，该路径用于保存绘图信息
            let pathRef = CGMutablePath()
            // 获取cell的size
            // 第一个参数,是整个 cell 的 bounds, 第二个参数是距左右两端的距离,第三个参数是距上下两端的距离
            let bounds = cell.bounds.insetBy(dx: 15, dy: 0)
            //用来显示中间线
            var addLine = false
            // CGRectGetMinY：返回对象顶点坐标
            // CGRectGetMaxY：返回对象底点坐标
            // CGRectGetMinX：返回对象左边缘坐标
            // CGRectGetMaxX：返回对象右边缘坐标
            // CGRectGetMidX: 返回对象中心点的X坐标
            // CGRectGetMidY: 返回对象中心点的Y坐标

            // 这里要判断分组列表中的第一行，每组section的第一行，每组section的中间行
            if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
                pathRef.addRoundedRect(in: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
            }else if indexPath.row == 0{
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY), transform: .identity)
                pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius, transform: .identity)
                pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius, transform: .identity)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY), transform: .identity)
//                addLine = true
            }else if indexPath.row == (tableView.numberOfRows(inSection: indexPath.section)-1) {
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY), transform: .identity)
                pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius, transform: .identity)
                pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius, transform: .identity)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY), transform: .identity)
            }else{
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY), transform: .identity)
                pathRef.addLine(to: CGPoint(x: 0, y: bounds.minY), transform: .identity)
                pathRef.addLine(to: CGPoint(x: 0, y: bounds.maxY), transform: .identity)
                pathRef.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY), transform: .identity)
                pathRef.move(to: CGPoint(x: bounds.maxX, y: bounds.minY), transform: .identity)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY), transform: .identity)
//                addLine = true
            }
            // 把已经绘制好的可变图像路径赋值给图层，然后图层根据这图像path进行图像渲染render
            layer.path = pathRef;
            // 注意：但凡通过Quartz2D中带有creat/copy/retain方法创建出来的值都必须要释放
//            CFRelease(pathRef);

            //颜色修改
            layer.fillColor = colorGray244.cgColor
            layer.strokeColor = colorGray244.cgColor
                //设置中间一根线，也可以不设置，看你的需求
//            if addLine == true {
//                let lineLayer = CALayer()
//                let lineHeight = 1.0 / UIScreen.main.scale
//                lineLayer.frame = CGRect(x: bounds.minX + 10, y: bounds.size.height - lineHeight, width: bounds.size.width - 10, height: lineHeight)
//                lineLayer.backgroundColor = UIColor.white.cgColor
//                layer.addSublayer(lineLayer)
//            }
            let testView = UIView(frame: bounds)
            testView.layer.insertSublayer(layer, at: 0)
            testView.backgroundColor = UIColor.white
            cell.backgroundView = testView
       }

}
